/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.teacher;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.config.Global;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.teacher.EduTeacherInfo;
import com.idenger.modules.edu.service.teacher.EduTeacherInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.persistence.Page;
import com.idenger.common.utils.StringUtils;

/**
 * 教师Controller
 * @author chenlin
 * @version 2017-11-28
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/teacher/eduTeacherInfo")
public class EduTeacherInfoController extends BaseController {

	@Autowired
	private EduTeacherInfoService eduTeacherInfoService;
	
	@ModelAttribute
	public EduTeacherInfo get(@RequestParam(required=false) String id) {
		EduTeacherInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduTeacherInfoService.get(id);
		}
		if (entity == null){
			entity = new EduTeacherInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:teacher:eduTeacherInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduTeacherInfo eduTeacherInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduTeacherInfo> page = eduTeacherInfoService.findPage(new Page<EduTeacherInfo>(request, response), eduTeacherInfo); 
		model.addAttribute("page", page);
		return "modules/edu/teacher/eduTeacherInfoList";
	}
	
	/*
	 *添加课程的时候，查询教师的列表数据，以及搜索的接口
	 *返回json数据 页面进行渲染 
	 * **/
	
	@RequiresPermissions("edu:teacher:eduTeacherInfo:view")
	@RequestMapping(value = "addTeacherToCourseList")
	public String addTeacherToCourse(EduTeacherInfo eduTeacherInfo, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		String name = request.getParameter("name");
		if(StringUtils.isNotBlank(name)){
			eduTeacherInfo.setName(name);
			model.addAttribute("name",name);
		}else{
			eduTeacherInfo.setName("");
		}
		List<EduTeacherInfo> list = eduTeacherInfoService.findList(eduTeacherInfo);
		if(list.size()>0){
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("teacherList",list);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("暂时没有教师");
		}
		return renderString(response, baseResponseMsg);
	}
	
	
	

	@RequiresPermissions("edu:teacher:eduTeacherInfo:view")
	@RequestMapping(value = "form")
	public String form(EduTeacherInfo eduTeacherInfo, Model model) {
		
		model.addAttribute("eduTeacherInfo", eduTeacherInfo);
		return "modules/edu/teacher/eduTeacherInfoForm";
	}

	@RequiresPermissions("edu:teacher:eduTeacherInfo:edit")
	@RequestMapping(value = "save")
	public String save(EduTeacherInfo eduTeacherInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eduTeacherInfo)){
			return form(eduTeacherInfo, model);
		}
		eduTeacherInfoService.save(eduTeacherInfo);
		addMessage(redirectAttributes, "保存教师成功");
		return "redirect:"+ Global.getAdminPath()+"/edu/teacher/eduTeacherInfo/?repage";
	}
	
	@RequiresPermissions("edu:teacher:eduTeacherInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(EduTeacherInfo eduTeacherInfo, RedirectAttributes redirectAttributes) {
		eduTeacherInfoService.delete(eduTeacherInfo);
		addMessage(redirectAttributes, "删除教师成功");
		return "redirect:"+Global.getAdminPath()+"/edu/teacher/eduTeacherInfo/?repage";
	}

}